<template>
  
<el-form ref="form" :model="form" :rules="rules2" label-width="200px">
  <el-form-item label="用户名">
    <el-input v-model="form.ADMIN_NAME"></el-input>
  </el-form-item>
  <el-form-item label="用户角色(仅针对销售部门)">
    <el-select v-model="form.ADMIN_ROLE" placeholder="请选择用户角色">
      <el-option label="销售员" value="销售员"></el-option>
      <el-option label="销售主管" value="销售主管"></el-option>
        <el-option label="销售助理" value="销售助理"></el-option>
          <el-option label="其他" value="其他"></el-option>
    </el-select>
  </el-form-item >
   <el-form-item label="密码" prop="ADMIN_PASSWORD" v-if="type=='新增'">
    <el-input type="password" v-model="form.ADMIN_PASSWORD" auto-complete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="ADMIN_CENTERPASSWORD" v-if="type=='新增'">
    <el-input type="password" v-model="form.ADMIN_CENTERPASSWORD" auto-complete="off"></el-input>
  </el-form-item>
   <el-form-item label="密码"  v-if="type=='查看'">
   <el-button type="danger" @click="passwordRest">
     重置密码
   </el-button>

  </el-form-item>
  <h4>权限选择</h4>
  <el-tree
  :data="menuList"
  show-checkbox 
  ref="tree"
   node-key="name"
  :expand-on-click-node="false"
  highlight-current
  :props="defaultProps">
</el-tree>
    <el-button type="primary" style="display: inherit; margin: 10px auto;" @click="onSubmit">确定</el-button>   
  </el-form-item>
</el-form>  
</template>

<script>
import api from "@/js/api";
import menu from "@/js/menu";
import getid from "@/js/getid";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.ADMIN_CENTERPASSWORD !== "") {
          this.$refs.form.validateField("ADMIN_CENTERPASSWORD");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
     
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value != this.form.ADMIN_PASSWORD) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {
        ADMIN_ID: "",
        ADMIN_NAME: "",
        ADMIN_PASSWORD: "",
        ADMIN_POWER: "",
        ADMIN_ROLE: "",
        ADMIN_CENTERPASSWORD: ""
      },
      rules2: {
        ADMIN_PASSWORD: [{ validator: validatePass, trigger: "blur" }],
        ADMIN_CENTERPASSWORD: [{ validator: validatePass2, trigger: "blur" }]
      },
      menuList: menu.menuList,
      defaultProps: {
        children: "content",
        label: "name"
      }
    };
  },
  props: {
    type: {
      type: String,
      default: ""
    },
    Id: {
      type: String,
      default: ""
    }
  },
  methods: {
    onSubmit() {
      this.form.ADMIN_POWER = this.$refs.tree.getCheckedKeys(true).join(",");
      let that = this;
      if (this.type == "查看") {
        api.UserUpdate(that, this.form);
      } else {
        this.form.ADMIN_ID = getid.getid();
        api.UserAdd(that, this.form);
      }
    },
    getCheckedNodes() {
      // console.log(this.$refs.tree.getCheckedNodes());
    },
    getDate() {
      var data = {
        Id: this.Id
      };
      let that = this;
      api.UserGetOne(that, data, function(msg) {
        that.form = msg.Result;
       
        if (that.form.ADMIN_POWER) {          
          that.form.ADMIN_POWER = that.form.ADMIN_POWER.split(",");
           that.$refs.tree.setCheckedKeys(that.form.ADMIN_POWER);
        }
       
      });
    },
    passwordRest() {
      var data = {
        ADMIN_ID: this.Id
      };
      let that = this;
      api.UserPassWordRest(that, data);
    }
  },
  created() {
    if (this.type != "新增") {
      this.getDate();
    }
  }
};
</script>